/*
 * $Id: Describe.java 23 2002-09-29 13:59:30Z bpruessmann $
 *
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.internal.core.api;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sourceforge.p4eclipse.core.api.ClientUser;
import net.sourceforge.p4eclipse.core.api.IPerforceCommand;
import net.sourceforge.p4eclipse.core.api.IDescribeResult;
import net.sourceforge.p4eclipse.core.syncinfo.ResourceSyncInfo;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;

/**
  * ClientUser object for the 'p4 describe' command.
  * 
  * @version 	$Revision: 23 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Describe extends ClientUser implements IPerforceCommand
{
	private Result result = new Result();
	private IStatus status;
	
	/**
	  * Convenience method for creating instances of Describe.
	  */
	public static final Describe newInstance()
	{
		MultiStatus status = new MultiStatus(PerforceUIPlugin.ID, IStatus.OK, "", null); //$NON-NLS-1$
		return new Describe(status);
	}
 
	/**
	  * Creates a new instance.
	  */
	private Describe(MultiStatus status)
	{		
		super(status);
		this.status = status;
	}
	
	/**
	  * @see IClientUser#outputStat(Map)
	  */
	public void outputStat(Map strDict)
	{
		result.changelist = Helper.safeGetInteger(strDict, "change"); //$NON-NLS-1$
		result.userName = Helper.safeGetString(strDict, "user"); //$NON-NLS-1$
		result.clientName = Helper.safeGetString(strDict, "client"); //$NON-NLS-1$
		result.date = Helper.safeGetDate(strDict, "time"); //$NON-NLS-1$
		result.description = Helper.safeGetString(strDict, "desc"); //$NON-NLS-1$
		result.status = Helper.safeGetString(strDict, "status"); //$NON-NLS-1$
		
		List affectedFiles = new ArrayList();
		
		int i = 0;
		while (strDict.containsKey("depotFile" + i)) //$NON-NLS-1$
		{
			ResourceSyncInfo resourceSyncInfo = new ResourceSyncInfo();
			resourceSyncInfo.setDepotFileName(Helper.safeGetString(strDict, "depotFile" + i)); //$NON-NLS-1$
			resourceSyncInfo.setAction(Helper.safeGetString(strDict, "action" + i)); //$NON-NLS-1$
			resourceSyncInfo.setHeadType(Helper.safeGetString(strDict, "type" + i)); //$NON-NLS-1$
			resourceSyncInfo.setHeadRev(Helper.safeGetInteger(strDict, "rev" + i)); //$NON-NLS-1$
			
			affectedFiles.add(resourceSyncInfo);
			i++;
		}
		
		result.affectedFiles = (ResourceSyncInfo[])affectedFiles.toArray(new ResourceSyncInfo[affectedFiles.size()]);
	}

	/**
	  * Returns the name of the p4 command of this instance.
	  */
	public String getCommand()
	{
		return "describe"; //$NON-NLS-1$
	}
	
	/**
	  * Returns the result of the operation.
	  */
	public IDescribeResult getResult()
	{
		return result;
	}
	
	/**
	  * Returns the IStatus object associated with the Dirs instance.
	  */
	public IStatus getStatus()
	{
		return status;
	}
	
	/** @see IPerforceCommand#isHidingFromConsole() */
	public boolean isHidingFromConsole()
	{
		return false;
	}	
	
	/**
	  * Implementation of the IDescribeResult. Stores information provided
	  * by the 'p4 describe' command.
	  * 
	  * @see IDescribeResult
	  */
	private static class Result implements IDescribeResult
	{
		private Integer changelist;
		private String userName;
		private String clientName;
		private Date date;
		private String description;
		private String status;
		private ResourceSyncInfo[] affectedFiles;
		
		/** Creates a new Result object. */
		private Result() { }
	
		/** @see IDescribeResult#getChangelist() */
		public Integer getChangelist() { return changelist; }
	
		/** @see IDescribeResult#getUsernName() */
		public String getUserName()	{ return userName; }
		
		/** @see IDescribeResult#getClientName() */
		public String getClientName() {	return clientName; }
	
		/** @see IDescribeResult#getDate() */
		public Date getDate() { return date; }
	
		/** @see IDescribeResult#getDescription() */
		public String getDescription() { return description; }
	
		/** @see IDescribeResult#getStatus() */
		public String getStatus() { return status; }
		
		/** @see IDescribeResult#getAffectedFiles() */
		public ResourceSyncInfo[] getAffectedFiles() { return affectedFiles; }	
	}	
}
